function Locations()
{
	var labbyContainer = $("#labby");
	this.initInfoPopup = function(response)
	{
		var html = $.parseHTML(response.html);
		labbyContainer.append(html);
		//Close function
		$(".more-info-popup .close-btn").on("click", function(){
			$(html).remove();
		});
	};
	this.initItemsPopup = function(response)
	{
		var html = $.parseHTML(response.html);
		labbyContainer.append(html);
		//Close function
		$(".items-show-popup .close-btn").on("click", function(){
			$(html).remove();
		});
	};
	
	this.saveLocation = function(location, afterSaveFx)
	{
		var filters = util.filters();
		util.appendFilter({val: location.description, name: "Descripción", filters: [filters.MAX_SIZE], extraval: 250});
		
		var afterFiltersFx = function()
		{
			var params = {
			location: location
			};
			var afterSave = function(response)
			{
				response = JSON.parse(response);
				if(util.handleResponseError(response))
				{
					return;
				}
				if(afterSaveFx)
				{
					afterSaveFx();
				}
			};
			$.post("locations/saveLocation", params, afterSave);
		};

		util.runFilters(afterFiltersFx);
		
	};
	this.deleteLocation = function(location, afterDeleteFx)
	{
		
		var params = {
			location: location
		};
		var afterDelete = function(response)
		{
			response = JSON.parse(response);
			if(util.handleResponseError(response))
			{
				return;
			}
			if(afterDeleteFx)
			{
				afterDeleteFx();
			}
		};
		$.post("locations/deleteLocation", params, afterDelete);
	};
	this.initEditPopup = function(response)
	{
		var thisObj = this;
		response.location = JSON.parse(response.location);
		var html = $.parseHTML(response.html);
		labbyContainer.append(html);
		var addOnChangeEvent = function(el, attr)
		{
			$(el).on("change", function(){
				response.location[attr] = $(this).val();
			});
			$(el).removeAttr("id");
		};
		//Form elements
		addOnChangeEvent($("#description"),"description");

		$(".edit-popup .save-btn").on("click", function(){
			var afterSave = function()
			{
				alert("El lugar fue guardado con éxito");
				util.refresh("/locations");
			};
			thisObj.saveLocation(response.location, function(){afterSave();});
		});
		//Close function
		$(".edit-popup .cancel-btn").on("click", function(){
			if(confirm("¿Está seguro?"))
			$(html).remove();
		});
	};
	this.initCreatePopup = function(response)
	{
		var thisObj = this;
		response.location = JSON.parse(response.location);
		var html = $.parseHTML(response.html);
		labbyContainer.append(html);
		response.location.description = "";
		var addOnChangeEvent = function(el, attr)
		{
			$(el).on("change", function(){
				response.location[attr] = $(this).val();
			});
			$(el).removeAttr("id");
		};
		//Form elements
		addOnChangeEvent($("#description"),"description");

		$(".create-popup .save-btn").on("click", function(){
			var afterSave = function()
			{
				alert("El lugar fue guardado con éxito");
				util.refresh("/locations");
			};
			thisObj.saveLocation(response.location, function(){afterSave();});
		});
		//Close function
		$(".create-popup .cancel-btn").on("click", function(){
			if(confirm("¿Está seguro?"))
			$(html).remove();
		});
	};
	this.showMoreInfo = function(id)
	{
		var thisObj = this;
		var afterLoad = function(response)
		{
		
			var responseObject = JSON.parse(response);
			if(util.handleResponseError(response))
			{
				return;
			}
			thisObj.initInfoPopup(responseObject);
		};
		var params = 
		{
			pk: id
		}
		$.post("locations/getLocationInfo", params, afterLoad);
	};

	this.showItems = function(id)
	{
		var thisObj = this;
		var afterLoad = function(response)
		{
		
			var responseObject = JSON.parse(response);
			if(util.handleResponseError(response))
			{
				return;
			}
			thisObj.initItemsPopup(responseObject);
		};
		var params = 
		{
			pk: id
		}
		$.post("locations/getLocationItems", params, afterLoad);
	};
	this.showEditLocation = function(id)
	{
		var thisObj = this;
		var afterLoad = function(response)
		{
		
			var responseObject = JSON.parse(response);
			if(responseObject.error)
			{
				alert(responseObject.message);
				return;
			}
			
			thisObj.initEditPopup(responseObject);
		};
		var params = 
		{
			pk: id
		}
		$.post("locations/getLocationEdit", params, afterLoad);
	};
	this.showDeleteLocation = function(id)
	{	
		if(confirm("¿Realmente quieres borrar este lugar?"))
		{
			var afterDeleteFx = function()
			{
				alert("El lugar fue eliminado con éxito.");
				util.refresh("/locations");
			};
			this.deleteLocation(id, afterDeleteFx);
		}
	};
	this.showCreateLocation = function()
	{
		var thisObj = this;
		var afterLoad = function(response)
		{
			if(util.handleResponseError(response))
			{
				return;
			}
			response = JSON.parse(response);
			thisObj.initCreatePopup(response);
		};
		$.post('locations/createLocation', {}, afterLoad);
	};
	this.init = function()
	{
		var thisObj = this;
		$("#locations-table tr").each(function()
		{
			var theTr = this;
			var id = $(theTr).attr("id");
			var moreBtn = $(theTr).find("a[class='btn more']");
			var editBtn = $(theTr).find("a[class='btn edit']");
			var deleteBtn = $(theTr).find("a[class='btn delete']");
			var showItemsBtn = $(theTr).find("a[class='btn items']");

			moreBtn.on("click", function()
			{
				thisObj.showMoreInfo(id);
			});
			showItemsBtn.on("click", function()
			{
				thisObj.showItems(id);
			});
			editBtn.on("click", function()
			{
				thisObj.showEditLocation(id);
			});
			deleteBtn.on("click", function()
			{
				thisObj.showDeleteLocation(id);
			});
			$(theTr).removeAttr("id");
		});
		
		$(".location-options .create").on("click", function(){
			thisObj.showCreateLocation();
		});
	};
	this.init();
}

var usr = new Locations();